define([
	'../libs/zepto',
	'../common/ajax',
	'../common/dialog',
	'../common/yingSoftPrg',
	'../common/yingSoftCookie',
	'../common/storage'
], function() {
	examTime();
})

function examTime() {
	var _showDate = new Date();
	var _examBeginTime = ''; //官方考试时间
	var _examTime = ''; //用户设置的考试时间

	var userID = G_Cookie.getUserID();

	if(userID) { //用户已登录，获取考试时间
		_getExamTime();
		
		if(_examBeginTime) {
			_showDate = _examBeginTime;
		}
		if(_examTime) {
			_showDate = _examTime;
		}
	}

	document.getElementById('year').innerHTML = _showDate.getFullYear();
	document.getElementById('month').innerHTML = _showDate.getMonth() + 1;
	_creatTimeLi(_showDate);
	_setClickEven();
	/********************************************************************************
	函数名：_dateAdd
	功能：
	输入参数：interval, number, date
	返回值：无
	创建信息：
	修改记录：无
	审查人：无
	*******************************************************************************/
	function _dateAdd(interval, number, date) {
		number = parseInt(number);
		if(typeof(date) == "string") {
			var date = new Date(date.split("-")[0], date.split("-")[1], date.split("-")[2]);
		}
		if(typeof(date) == "object") {
			var date = date;
		}
		switch(interval) {
			case "y":
				return new Date(date.getFullYear() + number, date.getMonth(), date.getDate());
				break;
			case "m":
				return new Date(date.getFullYear(), date.getMonth() + number, _checkDate(date.getFullYear(), date.getMonth() + number, date.getDate()));
				break;
			case "d":
				return new Date(date.getFullYear(), date.getMonth(), date.getDate() + number);
				break;
			case "w":
				return new Date(date.getFullYear(), date.getMonth(), 7 * number + date.getDate());
				break;
		}
	}
	/********************************************************************************
	函数名：_checkDate
	功能：获取当前年份当前月的天数
	输入参数：year 年份, month 月份, date 日
	返回值：returnDate 当前年份当前月的天数
	创建信息：
	修改记录：无
	审查人：无
	*******************************************************************************/
	function _checkDate(year, month, date) {
		var enddate = ["31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];
		var returnDate = "";
		if(year % 4 == 0) { //闰年天
			enddate[1] = "29";
		}
		if(date > enddate[month]) {
			returnDate = enddate[month];
		} else {
			returnDate = date;
		}
		return returnDate;
	}
	/********************************************************************************
	函数名：_weekDay
	功能：获取当前时间day
	输入参数：date 时间
	返回值：当前时间的日
	创建信息：
	修改记录：无
	审查人：无
	*******************************************************************************/
	function _weekDay(date) {
		var theDate;
		if(typeof(date) == "string") {
			theDate = new Date(date.split("-")[0], date.split("-")[1], date.split("-")[2]);
		}
		if(typeof(date) == "object") {
			theDate = date;
		}
		return theDate.getDay();
	}
	/********************************************************************************
	函数名：_creatTimeLi
	功能：生成日历列表
	输入参数：date 当前日期
	返回值：无
	创建信息：
	修改记录：无
	审查人：无
	*******************************************************************************/
	function _creatTimeLi(date) {
		var lis = "";
		var now;
		if(typeof(date) == "string") {
			selectDate = date.split("-");
			var year = selectDate[0];
			var month = parseInt(selectDate[1]) - 1 + "";
			var date = selectDate[2];
			now = new Date(year, month, date);
		} else if(typeof(date) == "object") {
			now = date;
		}
		var lastMonthEndDate = _dateAdd("d", "-1", now.getFullYear() + "-" + now.getMonth() + "-01").getDate();
		var lastMonthDate = _weekDay(now.getFullYear() + "-" + now.getMonth() + "-01");
		var thisMonthLastDate = _dateAdd("d", "-1", now.getFullYear() + "-" + (parseInt(now.getMonth()) + 1).toString() + "-01");
		var thisMonthEndDate = thisMonthLastDate.getDate();
		var thisMonthEndDay = thisMonthLastDate.getDay();
		var todayObj = new Date();
		today = todayObj.getFullYear() + "/" + (parseInt(todayObj.getMonth()) + 1) + "/" + todayObj.getDate();
		if(_examTime) {
			var nowExamTime = _examTime.getFullYear() + "/" + _examTime.getMonth() + "/" + _examTime.getDate();
		}
		if(_examBeginTime) {
			var examBeginTime = _examBeginTime.getFullYear() + "/" + _examBeginTime.getMonth() + "/" + _examBeginTime.getDate();
		}
		G_Prg.$('dateUl').innerHTML = '';
		for(i = 0; i < lastMonthDate; i++) { // Last Month's Date
			var liTag = document.createElement('li');
			liTag.className = 'lastMonthDate';
			liTag.innerHTML = lastMonthEndDate - lastMonthDate + i + 1;
			G_Prg.$('dateUl').appendChild(liTag);
		}
		for(i = 1; i <= thisMonthEndDate; i++) { // Current Month's Date
			var liTag = document.createElement('li');
			var date = now.getFullYear() + "/" + (parseInt(now.getMonth()) + 1) + "/" + i;
			liTag.id = date;
			if(new Date(date) >= new Date(today)) {
				liTag.onclick = function() {
					$('#save').addClass('bg_save');
					_selectThisDay(this.id);
				};
			} else {
				liTag.onclick = function() {
					G_Prg.popMsg('请选择今天以后的日期', 2000);
				};
			}

			liTag.className = 'day';
			liTag.innerHTML = i;
			if(today === now.getFullYear() + "/" + (parseInt(now.getMonth()) + 1) + "/" + i) {
				liTag.className = 'today';
			}
			if(_examTime && nowExamTime === now.getFullYear() + "/" + now.getMonth() + "/" + i) {
				liTag.className = 'examTime';
			}
			if(_examBeginTime && examBeginTime === now.getFullYear() + "/" + now.getMonth() + "/" + i) {
				liTag.className = 'examBeginTime';
			}
			G_Prg.$('dateUl').appendChild(liTag);
		}
		var j = 1;
		for(i = thisMonthEndDay; i < 6; i++) { // Next Month's Date
			var liTag = document.createElement('li');
			liTag.className = 'nextMonthDate';
			liTag.innerHTML = j;
			G_Prg.$('dateUl').appendChild(liTag);
			j++;
		}
	}
	/********************************************************************************
	函数名：_selectThisDay
	功能：选中某个日期的点击事件
	输入参数：id 被选中日期的id
	返回值：无
	创建信息：
	修改记录：无
	审查人：无
	*******************************************************************************/
	function _selectThisDay(id) {
		var obj = document.getElementsByClassName('examTime');
		for(var i = 0; i < obj.length;) {
			obj[i].className = 'day';
		}
		G_Prg.$(id).className = 'examTime';
		var examTime = new Date(id);
		G_Prg.$('save').onclick = function() {
			_saveClick(examTime);
		}
	}
	/********************************************************************************
	函数名：_setClickEven
	功能：初始化点击事件
	输入参数：无
	返回值：无
	创建信息：
	修改记录：无
	审查人：无
	*******************************************************************************/
	function _setClickEven() { //初始化点击事件
		$('.back').click(function() {
			window.location.href = '/doExam-index';
		});
		document.getElementById('lastYear').onclick = function() {
			var year = parseInt(document.getElementById('year').innerHTML);
			var month = parseInt(document.getElementById('month').innerHTML);
			year--;
			var day = _checkDate(year, month, 1);
			var date = year + '-' + month + '-' + day;
			_creatTimeLi(date);
			document.getElementById('year').innerHTML = year;
		}

		document.getElementById('nextYear').onclick = function() {
			var year = parseInt(document.getElementById('year').innerHTML);
			var month = parseInt(document.getElementById('month').innerHTML);
			year++;
			var day = _checkDate(year, month, 1);
			var date = year + '-' + month + '-' + day;
			_creatTimeLi(date);
			document.getElementById('year').innerHTML = year;
		}

		document.getElementById('nextMonth').onclick = function() {
			var year = parseInt(document.getElementById('year').innerHTML);
			var month = parseInt(document.getElementById('month').innerHTML);
			if(month < 12) {
				month = month + 1;
			} else {
				month = 1;
				year = year + 1;
			}
			var day = _checkDate(year, month, 1);
			var date = year + '-' + month + '-' + day;
			_creatTimeLi(date);
			document.getElementById('year').innerHTML = year;
			document.getElementById('month').innerHTML = month;
		}
		document.getElementById('lastMonth').onclick = function() {
			var year = parseInt(document.getElementById('year').innerHTML);
			var month = parseInt(document.getElementById('month').innerHTML);
			if(month === 1) {
				month = 12;
				year = year - 1;
			} else {
				month = month - 1;
			}
			var day = _checkDate(year, month, 1);
			var date = year + '-' + month + '-' + day;
			_creatTimeLi(date);
			document.getElementById('year').innerHTML = year;
			document.getElementById('month').innerHTML = month;

		}
	}

	/********************************************************************************
	函数名：_saveClick
	功能：保存按钮点击事件（若用户已登陆，将填写的数据发送至服务器）
	输入参数：无
	返回值：无
	创建信息：
	修改记录：无
	审查人：无
	*******************************************************************************/
	function _saveClick(examTime) {
		if(G_Storage.checkSessionStorageSupport) {
			G_Storage.setSessionStorageValue('showTime', 0);
		}
		var userID = G_Cookie.getUserID();

		if(!userID) {
			window.location.href = 'doExam-index';
			return;
		}
		var time = examTime;
		var URL = '/api/examTime/setExamTimeData';
		var data = {
			appID: G_Cookie.getAppID(),
			examTime: time,
			guid: G_Cookie.getGuid()
		};
		G_AjaxApi_Record.post(URL, data, false, function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {
				window.location.href = '/doExam-index';
			} else if(jsonData.status === 201) {
				G_Prg.throw('添加考试时间失败');
			} else if(jsonData.status === 300) {
				G_Prg.throw('程序运行错误：examTime._saveClick，数据库连接失败');
			} else if(jsonData.status === 400) {
				G_Prg.throw('程序运行错误：examTime._saveClick，参数错误');
			} else {
				G_Prg.throw('程序运行错误：examTime._saveClick，不能处理status=' + jsonData.status);
			}
		});
	}

	function _getExamTime() {
		var userID = G_Cookie.getUserID();
		var appID = G_Cookie.getAppID();
		var guid = G_Cookie.getGuid();
		var userName = G_Cookie.getUserName();

		var URL = '/api/examTime/getExamTimeData?appID=' + appID + '&guid=' + guid;

		G_AjaxApi_Record.get(URL, false, function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {
				if(jsonData.data.examBeginTime) {
					_examTime = new Date(jsonData.data.examBeginTime);
				} else {
					if(jsonData.data.examTime){
						_examTime = new Date(jsonData.data.examTime);
					}else{
						_examTime = new Date();
					}
				}
			} else if(jsonData.status === 201) {
				_examBeginTime = new Date(jsonData.examBeginTime);
			} else if(jsonData.status === 300) {
				G_Prg.throw('程序运行错误:G_Prg.getExamTime,数据库连接错误');
			} else if(jsonData.status === 400) {
				G_Prg.throw('程序运行错误:G_Prg.getExamTime,参数错误');
			} else {
				G_Prg.throw('程序运行错误:G_Prg.getExamTime  未知返回值：' + JSON.stringify(jsonData));
			}
		});
	}
}